package org.come.Jpanel;

import com.updateNew.MyIsif;
import org.come.bean.ConfigureBean;
import org.come.model.Configure;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Map;

public class ActivityWeekJpanel extends JPanel {


    public static String title = "活动周报";

    public static String[] weekText = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    public static String[] weekDateText = new String[7];

    public static int week = 0;
    public static String weekData = "";

    public ActivityWeekJpanel() {
        setPreferredSize(new Dimension(855, 416));
        setOpaque(false);
        setLayout(null);
        setWeekText();
    }


    private ImageIcon icon;
    private ImageIcon icon1;
    private ImageIcon icon2 = new ImageIcon("skin/8100226/newbutton/日历小格子2.png");
    private ImageIcon icon3 = new ImageIcon("skin/8100226/newbutton/日历小格子1.png");
    private ImageIcon icon4 = new ImageIcon("skin/8100226/newbutton/日历小格子3.png");
    private ImageIcon icon5 = new ImageIcon("skin/8100226/newbutton/框子001.png");
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (icon == null) {
                icon = new ImageIcon("inkImg/hongmu/week003.png");
            }
            if (icon1 == null) {
                icon1 = new ImageIcon("skin/8100226/newbutton/week002.png");
            }
        } else {
            if (icon == null) {
                icon = new ImageIcon("skin/8100226/newbutton/week001.png"); // 替换为其他风格的图标路径
            }
            if (icon1 == null) {
                icon1 = new ImageIcon("skin/8100226/newbutton/week002.png"); // 替换为其他风格的图标路径
            }
        }
        g.drawImage(icon.getImage(), 42, 0, 815, 418, this);
        g.drawImage(icon1.getImage(), 40, 10, 780, 372, this);
        g.drawImage(icon5.getImage(), 360, 11, 170, 25, this);
        g.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        g.setColor(Color.white);
        g.drawString(title, 375, 30);
        String[] activityText = weekData.split("\\|");
        for (int i = 0; i < 7; i++) {
            g.setFont(new Font("宋体", Font.BOLD, 16));
            if(i<5){
                g.setColor(Color.black);
            }else{
                g.setColor(Color.red);
            }
            g.drawImage(icon2.getImage(), 80 + (i * 98), 40, 96, 44, this);
            g.drawString(weekText[i], 110 + (i * 98), 60);
            if(weekDateText[i]!=null){
                g.drawString(weekDateText[i], 107 + (i * 98), 78);
            }
            if(week==i){
                //今日
//                g.drawString("今\t日", 130 + (i * 98), 60);
            }
            if(activityText[i]!=null){
                String[] activity = activityText[i].split("\\$");
                for(int j=0;j<activity.length;j++){
                    if(i == week){
                        g.drawImage(icon4.getImage(), 80 + (i * 98), 85+(j*46), 96, 44, this);
                    }else{
                        g.drawImage(icon3.getImage(), 80 + (i * 98), 85+(j*46), 96, 44, this);
                    }
                    if(activity[j]!=null){
                        g.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                        if(i == week){
                            g.setColor(Color.black);
                        }else{
                            g.setColor(Color.gray);
                        }

                        String[] activity1 = activity[j].split("\\&");
                        // 获取字符串宽度并居中绘制
                        FontMetrics fontMetrics = g.getFontMetrics();
                        int textWidth0 = fontMetrics.stringWidth(activity1[0]);
                        int textWidth1 = fontMetrics.stringWidth(activity1[1]);

                        int x0 = 80 + (i * 98) + (96 - textWidth0) / 2; // 居中绘制第一个字符串
                        int x1 = 80 + (i * 98) + (96 - textWidth1) / 2; // 居中绘制第二个字符串
                        int y0 = 85 + (j * 46);
                        int y1 = 85 + 20 + (j * 46);

                        g.drawString(activity1[0], x0, y0 + 20);
                        g.drawString(activity1[1], x1, y1 + 16);
                    }
                }
            }
        }
    }



    public void setWeekText() {
        ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
        Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
        Configure configure1 = item.get(new BigDecimal(2));
        weekData = configure1.getZqsld();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("MM月-dd日");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 调整到本周的周一
        LocalDate startOfWeek = currentDate.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
        // 调整到本周的周日
        LocalDate endOfWeek = currentDate.with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));
        // 格式化输出
        String formattedStartOfWeek = startOfWeek.format(formatter1);
        String formattedEndOfWeek = endOfWeek.format(formatter1);
        week = currentDate.getDayOfWeek().getValue() - 1;
        title = formattedStartOfWeek + " - " + formattedEndOfWeek;

        for (int i = 0; i < 7; i++) {
            LocalDate day = startOfWeek.plusDays(i);
            weekDateText[i] = day.format(formatter);
        }
    }

}
